//
//  SNSPhotoFilterViewController.m
//  iBaby365v2
//
//  Created by Kirinrin on 14-8-13.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "SNSPhotoFilterViewController.h"
#import "SNSPhotoEditViewController.h"

#define kFilterPreviewImageSize     124

@interface SNSPhotoFilterViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic)UIImage *preViewImage;

@property (strong, nonatomic)NSArray *items;
@end

@implementation SNSPhotoFilterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.imageView.image = self.image;
    
    self.items = @[@{@"name":@"无", @"filter":@"Original"},
                   @{@"name":@"怀旧",  @"filter":@"CILinearToSRGBToneCurve"},
                   @{@"name":@"铬黄",  @"filter":@"CIPhotoEffectChrome"},
                   @{@"name":@"岁月",  @"filter":@"CIPhotoEffectFade"},
                   @{@"name":@"冲印",  @"filter":@"CIPhotoEffectInstant"},
                   @{@"name":@"单色",  @"filter":@"CIPhotoEffectMono"},
                   @{@"name":@"黑白",  @"filter":@"CIPhotoEffectNoir"},
                   @{@"name":@"经典",  @"filter":@"CIPhotoEffectProcess"},
                   @{@"name":@"色调",  @"filter":@"CIPhotoEffectTonal"},
                   @{@"name":@"传递",  @"filter":@"CIPhotoEffectTransfer"},
                   ];
    
    _preViewImage = [self preViewImage:self.image withSize:CGSizeMake(kFilterPreviewImageSize, kFilterPreviewImageSize)];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UIImage *)filterImage:(UIImage *)orgImage withFilter:(NSString *)filterName{
    CIImage *ciImage = [[CIImage alloc] initWithImage:orgImage];
    
    CIFilter *filter = [CIFilter filterWithName:filterName
                                  keysAndValues:kCIInputImageKey, ciImage, nil];
    [filter setDefaults];
    
    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *outputImage = [filter outputImage];
    CGImageRef cgImage = [context createCGImage:outputImage
                                       fromRect:[outputImage extent]];
    
    return [UIImage imageWithCGImage:cgImage];
    
    CGImageRelease(cgImage);
    
}

-(UIImage *)preViewImage:(UIImage *)source withSize:(CGSize) size{
    UIGraphicsBeginImageContext(size);
    [source drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *preViewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return preViewImage;
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"toPhotoEdit"]) {
        SNSPhotoEditViewController *controller = segue.destinationViewController;
        UIImage *filteredImage = self.imageView.image;
        controller.sourceImage = filteredImage;
        controller.delegate = self.tempDelegate;
    }
    
}


- (IBAction)closeViewAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - fileter collection datasource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.items.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *filter = self.items[indexPath.item];
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"filterCell" forIndexPath:indexPath];
    UILabel *label = (UILabel *)[cell viewWithTag:101];
    label.text = filter[@"name"];
    
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
    if ([filter[@"filter"]isEqualToString:@"Original"]) {
        imageView.image = self.preViewImage;
    }else{
        imageView.image = [self filterImage:self.preViewImage withFilter:filter[@"filter"]];
    }
    
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *filter = self.items[indexPath.item];
    if ([filter[@"filter"]isEqualToString:@"Original"]) {
        self.imageView.image = self.image;
    }else{
        self.imageView.image = [self filterImage:self.image withFilter:filter[@"filter"]];
    }

}
@end
